//package com.io;
//
//import okhttp3.*;
//
//import java.io.*;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//public class HtmlCrawl2 {
//
//    private static OkHttpClient client = new OkHttpClient.Builder()
//            .connectTimeout(600, TimeUnit.SECONDS)
//            .readTimeout(600, TimeUnit.SECONDS)
//            .writeTimeout(600, TimeUnit.SECONDS)
//            .cookieJar(new CookieJar() {
//                private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();
//
//                @Override
//                public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
//                    cookieStore.put(url, cookies);
//                }
//
//                @Override
//                public List<Cookie> loadForRequest(HttpUrl url) {
//                    List<Cookie> cookies = cookieStore.get(url);
//                    return cookies != null ? cookies : new ArrayList<Cookie>();
//                }
//            })
//            .build();
//
//
//    public static void main(String[] args) {
//        File html = new File("/home/zhuangqi/Documents/project/FileDownload.aspx.html");
//        InputStream in = null;
//        ByteArrayOutputStream out = null;
//
//        try {
//            in = new FileInputStream(html);
//            out = new ByteArrayOutputStream();
//            byte[] buf = new byte[2048];
//            int byteRead = 0;
//            while ((byteRead = in.read(buf))!=-1){
//                out.write(buf,0,byteRead);
//            }
//            String htmlContext = new String(out.toByteArray(),"ISO-8859-1");
//            String regex = "(?<=DownLoadFile\\()[^\\)]+";
//            Matcher matcher = Pattern.compile(regex).matcher(htmlContext);
//            List<String> matchList = new ArrayList<>();
//            List<Map<String,String>> downParams = new ArrayList<>();
//            while (matcher.find()){
//                matchList.add(matcher.group());
//            }
//            matchList.remove(matchList.size()-1);
//            matchList.remove(matchList.size()-2);
//            for (String s : matchList) {
//                String[] params = s.split(",");
//                Map<String,String> param = new HashMap<>();
//                param.put("id",params[0]);
//                param.put("dates",params[1]);
//                downParams.add(param);
//            }
//
//            RequestBody loginBody = new FormBody.Builder()
//                    .add("action","2")
//                    .add("userName","chongqing")
//                    .add("pwd","chongqingcpws")
//                    .build();
//            Request request1 = new Request.Builder()
//                    .url("http://wenshu.court.gov.cn/download/FileDownload.aspx")
//                    .post(loginBody).build();
//
//            Response response1 = client.newCall(request1).execute();
//            if (!response1.isSuccessful()) throw new IOException("Unexpected code " + response1);
//            System.out.println(response1.body().string());
//
//            Runnable myRunable1 = new Runnable() {
//                @Override
//                public void run() {
//                    for (int i = 0; i < downParams.size()/2; i++) {
//                        downFile(downParams.get(i).get("id"),downParams.get(i).get("dates"));
//                    }
//                }
//            };
//
//            Runnable myRunable2 = new Runnable() {
//                @Override
//                public void run() {
//                    for (int i = downParams.size()/2; i < downParams.size(); i++) {
//                        downFile(downParams.get(i).get("id"),downParams.get(i).get("dates"));
//                    }
//                }
//            };
//
//            Thread thread1 = new Thread(myRunable1);
//            Thread thread2 = new Thread(myRunable2);
//
//            thread1.start();
//            thread2.start();
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                if (in!=null){
//                    in.close();
//                }
//                if (out!=null){
//                    out.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    private static void downFile(String id,String dates){
//        InputStream inputStream = null;
//        FileOutputStream fileOutputStream = null;
//        BufferedOutputStream bufferedOutputStream = null ;
//        RequestBody formBody = new FormBody.Builder()
//                .add("action","1")
//                .add("id",id)
//                .add("dates",dates).build();
//        Request request2 = new Request.Builder()
//                .url("http://wenshu.court.gov.cn/download/FileDownload.aspx")
//                .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
//                .addHeader("Accept-Encoding","gzip, deflate")
//                .addHeader("Content-Type","application/x-www-form-urlencoded")
//                .addHeader("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
//                .post(formBody)
//                .build();
//
//        try {
//            Response response2 = client.newCall(request2).execute();
//            if (!response2.isSuccessful()) throw new IOException("Unexpected code " + response2);
//            String fileName = response2.header("Content-Disposition").substring(21).replace("/","-").replace(" ","");
//            File file = new File("/home/zhuangqi/Documents/project/wenshu/"+fileName);
//            inputStream = response2.body().byteStream();
//            fileOutputStream = new FileOutputStream(file);
//            bufferedOutputStream = new BufferedOutputStream( fileOutputStream ) ;
//            int len = 0;
//            byte[] buffer = new byte[4096];
//            while ((len = inputStream.read(buffer)) != -1) {
//                bufferedOutputStream.write(buffer, 0, len);
//            }
//
//            bufferedOutputStream.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }finally {
//            if (inputStream!=null){
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            if (fileOutputStream!=null){
//                try {
//                    fileOutputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
//}
